var GameData = require('gameData')();


function fishfactory() {

    this.bezierArr = [];
    /**
     *
     * @param id  鱼id
     * @param x    鱼x
     * @param y     鱼y
     * @param fishPrefab 鱼的预制体
     * @param _parentNode  鱼放在哪个层上
     * @param fishArr  存储鱼的数组
     * @param type     鱼的类型判断是特殊鱼还是普通鱼
     * @param type1     判断是home 场景中加载还是game场景加载
     */
    this.createfish = function (id, x, y, fishPrefab, _parentNode, fishArr, type, type1, bezierid) {
        let _Allfish = null;
        let _fish = null;
        // id = 12
        let _fishPrefab = fishPrefab[id];
        /*获取具体那条鱼的预制体*/
        // for (var a = 0; a < GameData.Allfish_pool._pool.length; a++) {
        //     if (GameData.Allfish_pool._pool[a].id == id) {
        //         _Allfish = GameData.Allfish_pool._pool[a];
        //     }
        // }
        // if (_Allfish) { // 通过 size 接口判断对象池中是否有空闲的对象
        //     GameData.fish_Pool.put(_Allfish.getChildByName("fish"));
        //     if (GameData.fish_Pool.size() > 0) {
        //         _fish = GameData.fish_Pool.get();
        //     } else {
        //         _fish = cc.instantiate(_fishPrefab);
        //     }
        //     _fish.parent = _Allfish;
        //     _fish.x = 0;
        //     _fish.y = 0;
        // } else { // 如果没有空闲对象，也就是对象池中备用对象不够时，我们就用 cc.instantiate 重新创建
        //     _Allfish = cc.instantiate(Allfishprefab);
        //     if (GameData.fish_Pool.size() > 0) {
        //         _fish = GameData.Allfish_pool.get();
        //     } else {
        //         _fish = cc.instantiate(_fishPrefab);
        //     }
        //     _fish.parent = _Allfish;
        //     _fish.x = 0;
        //     _fish.y = 0;
        //     // _Allfish.getChildByName("fish").getChildByName("fishid").getComponent(cc.Label).string = "鱼" + id1;
        // }

        _Allfish = cc.instantiate(_fishPrefab);
        _Allfish.stopAllActions();
        let mess = GameData.getData(GameData.hashData[0], id);
        _Allfish.ph = mess.ph;
        _Allfish.maxph = mess.maxph;
        _Allfish.price = mess.price;
        _Allfish.id = id;
        _Allfish.backMove = false;
        _Allfish.iseat = false;
        _Allfish.movidx = 0;
        _Allfish.x = x;
        _Allfish.y = y;
        _Allfish.sceneType = type1;
        _Allfish.ischarged = false;
        _Allfish.moveStop = true;
        if( bezierid){
            _Allfish.bezierid = bezierid
        }else {
            _Allfish.bezierid = this.getbezierid(fishArr, type);
        }


        _Allfish.ischeck = true;
        _Allfish.isgo = false;
        _Allfish.clickPos = null;
        _Allfish.playXY = true;
        /*播放收到雷击的状态*/
        if (type) {
            _Allfish.x = x;
            _Allfish.y = y;
        }
        if (!type1) {
            var getlocakdata = GameData.getlocalData("fishData");
            if(GameData.GameLayer){
                var newArr1=[];
                for(var a=0;a<GameData.GameLayer.catchFish.length;a++){
                    newArr1.push(GameData.GameLayer.catchFish[a].id)
                }

                var newArr=getlocakdata.handbook.oldfish.concat(newArr1);
                if (newArr.indexOf(id) == -1) {
                    _Allfish.getChildByName("newfish").active = true;
                } else {
                    _Allfish.getChildByName("newfish").active = false;
                }
            }
        }else {
            _Allfish.getChildByName("newfish").active = false;
        }
        if (id == 12 || id == 13 || id == 14) {
            _Allfish.x = Math.floor(Math.random() * 200 + 100);
            _Allfish.y = -330;
        } else {
            // GameData.GameLayer.getMovePath(_Allfish, "bezier_id_" + bezierid);
        }

        fishArr.push(_Allfish);
        _Allfish.parent = _parentNode;
        if (bezierid) {
            return _Allfish;
        }

    };
    this.getbezierid = function (fishArr, type) {
        // moveid "bezier_id_"
        let _Arr = [];
        if (type) {
            for (var a = 11; a < 22; a++) {
                _Arr.push(a);
            }
        } else {
            for (var a = 0; a < 10; a++) {
                _Arr.push(a);
            }
        }

        for (var a = 0; a < fishArr.length; a++) {
            for (var b = 0; b < _Arr.length; b++) {
                if (fishArr[a].moveid == "bezier_id_" + _Arr[b]) {
                    _Arr.splice(b, 1);
                    b--;
                    break;
                }
            }
        }
        if (_Arr.length == 0) {
            var bezierid = Math.floor(Math.random() * 10);
            this.bezierArr.push(bezierid);
            return bezierid;
        } else {
            var bezierid = Math.floor(Math.random() * _Arr.length);
            this.bezierArr.push(_Arr[bezierid]);
            return _Arr[bezierid];
        }

    };

    /**
     *回收鱼
     *
     * @param {*} node 鱼节点
     */
    this.poolFish = function (node) {
        console.log("回收鱼");
        GameData.Allfish_pool.put(node);
    }


};
var _instance = null;
module.exports = function () {
    if (!_instance) {
        _instance = new fishfactory();
    }
    return _instance;
};